import Dep from "./Dep.js"
import { observe } from "./index"

export default function defineReactive(data,key,val) {
    // let dep = new Dep()
    if(arguments.length == 2){
        val = data[key]
    }
    let dep = new Dep
   
    let childOb = observe(val)
    Object.defineProperty(data,key,{
        enumerable:true,
        configurable:true,
        get(){
            // console.log('访问'+key+'属性');
            // 如果现在处于依赖的收集阶段
            if(Dep.target){
                dep.depend()
                if(childOb){
                    childOb.dep.depend()
                }
            }
            return val
        },
        set(newVal){
            if(val === newVal) return
            // console.log('修改'+key+'属性');
            val = newVal
            childOb = observe(newVal)
            dep.notify()
        }
    })
}
